package com.forum.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.forum.article.entity.ForumArticleTag;
import com.forum.article.entity.ForumArticleTagMap;
import com.forum.article.mapper.ForumArticleTagMapMapper;
import com.forum.article.mapper.ForumArticleTagMapper;
import com.forum.article.service.ForumArticleTagService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.article.vo.HotTagVo;
import com.forum.article.vo.Result;
import com.forum.article.vo.TagVO;
import com.forum.common.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.swing.text.html.HTML;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bncgnc
 * @since 2022-01-04
 */
@Service
public class ForumArticleTagServiceImpl extends ServiceImpl<ForumArticleTagMapper, ForumArticleTag> implements ForumArticleTagService {
    @Autowired
    ForumArticleTagMapper forumArticleTagMapper;
    @Autowired
    ForumArticleTagMapMapper forumArticleTagMapMapper;




    public TagVO copy(ForumArticleTag forumArticleTag){
        TagVO tagVO = new TagVO();
        BeanUtils.copyProperties(forumArticleTag,tagVO);
        tagVO.setId(forumArticleTag.getId());
        return tagVO;
    }

    public List<TagVO> copyList(List<ForumArticleTag> tagList){
        List<TagVO> tagVOList = new ArrayList<>();
        for(ForumArticleTag forumArticleTag:tagList){
            tagVOList.add(copy(forumArticleTag));
        }
        return tagVOList;
    }


    @Override
    public List<TagVO> findTagsByArticleId(Long articleId) {
        List<ForumArticleTag> forumArticleTags = forumArticleTagMapper.findTagsByArticleId(articleId);

        return copyList(forumArticleTags);
    }

    @Override
    public R hots(int limit) {
        /**
        *1.标签所拥有的文章数量最多
        *2.查询 根据tag_id 分组计数，从大到小排列
         */
        List<HotTagVo> hotTagVos = forumArticleTagMapMapper.selectHotTag(limit);

        if (hotTagVos != null && hotTagVos.size() <= limit) {
            List<Long> hotTagIds = hotTagVos.stream().map(ht -> ht.getArticleTagId()).collect(Collectors.toList());

//            hotTagIds.forEach(System.out::println);

            List<ForumArticleTag> hotTags = forumArticleTagMapper.selectBatchIds(hotTagIds);

            return R.ok().put("success" , true).put("data" , hotTags);
        } else {
            return R.ok().put("success" , true).put("data" , null);
        }

//        List<Long> tagIds = forumArticleTagMapper.findHotsTagIds(limit);
//        if (CollectionUtils.isEmpty(tagIds)){
//            return Result.success(Collections.emptyList());
//        }

//        List<ForumArticleTag> tagList = forumArticleTagMapper.findTagsByTagIds(tagIds);
    }

    @Override
    public Result findAll() {
        LambdaQueryWrapper<ForumArticleTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(ForumArticleTag::getId,ForumArticleTag::getTagName);
        List<ForumArticleTag> forumArticleTags = this.forumArticleTagMapper.selectList(queryWrapper);
        return Result.success(copyList(forumArticleTags));
    }


    public Result findAllDetail(){
        LambdaQueryWrapper<ForumArticleTag> queryWrapper = new LambdaQueryWrapper<>();
        List<ForumArticleTag> forumArticleTags = this.forumArticleTagMapper.selectList(queryWrapper);
        return Result.success(copyList(forumArticleTags));
    }

    @Override
    public Result findDetailById(Long id) {
        ForumArticleTag forumArticleTag = forumArticleTagMapper.selectById(id);

        return Result.success(copy(forumArticleTag));
    }
}
